package com.zdkj.subway.st.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.st.entity.SwStInputDetail;
import com.zdkj.subway.st.entity.SwStInvalidTicket;
import com.zdkj.subway.st.entity.SwStOutputDetail;
import com.zdkj.subway.st.mapper.SwStInvalidTicketMapper;
import com.zdkj.subway.st.service.SwStInvalidTicketService;
import com.zdkj.subway.st.util.DateUtilsSt;
import com.zdkj.subway.st.util.GenSerialNo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ProjectName: zdkj
 * @Package: com.zdkj.subway.st.service.impl
 * @ClassName: SwStInvalidTicketServiceImpl
 * @Description:
 * @Author: kangjian
 * @CreateDate: 2020/1/7 13:55
 * @Version: 1.0
 */

@Service
@AllArgsConstructor
public class SwStInvalidTicketServiceImpl extends ServiceImpl<SwStInvalidTicketMapper, SwStInvalidTicket> implements SwStInvalidTicketService {

    private final SwStInputDetailServiceImpl swStInputDetailService;
    private final SwStOutputDetailServiceImpl swStOutputDetailService;
    @Resource
    SwStInputDetailServiceImpl inputDetailServiceImpl;
    @Resource
    SwStOutputDetailServiceImpl outputDetailServiceImpl;
    @Autowired
    private GenSerialNo genSerialNo;
    @Override
    public boolean inputDetailSubmitBatch(List<Integer> ids){
        PigxUser user = SecurityUtils.getUser();
        Collection<SwStInvalidTicket> swStInvalidTicketList = listByIds(ids);
        List<SwStInputDetail> list = new ArrayList<>();
        boolean isSucss = true;
        for(SwStInvalidTicket item :swStInvalidTicketList){
            //0:未出库 1:已入库
            item.setInputStatus("1");
            //入库
            SwStInputDetail swStInputDetail = new SwStInputDetail();
            swStInputDetail.setInGoingType("9");
            swStInputDetail.setInGoingTime(DateUtilsSt.newLocalDateTime());
            swStInputDetail.setTicketType(item.getTicketType());
            swStInputDetail.setTicketName(item.getTicketName());
            swStInputDetail.setTicketTimes(0);
            swStInputDetail.setTicketPrice(new BigDecimal("0.00"));
            swStInputDetail.setTicketNumber(1);
            swStInputDetail.setTicketNo(item.getOldCardOrder());
            swStInputDetail.setKeeper1code(user.getUserCode());
            swStInputDetail.setKeeper1name(user.getNikeName());
            swStInputDetail.setRemark(item.getRemark());
            swStInputDetail.setDataSource("1");
            swStInputDetail.setActiveTime(item.getActiveTime());
            //入库生成单号和更新基础表信息
            inputDetailServiceImpl.inputGenerateSingle(swStInputDetail);
            list.add(swStInputDetail);
            //isSucss = swStInputDetailService.save(swStInputDetail);
        }
        //用于更新是否操作入库
        updateBatchById(swStInvalidTicketList);
        return swStInputDetailService.saveBatch(list);
    }

    @Override
    public boolean outDetailSubmitBatch(List<Integer> ids){
        PigxUser user = SecurityUtils.getUser();
        Collection<SwStInvalidTicket> swStInvalidTicketList = listByIds(ids);
        List<SwStOutputDetail> list = new ArrayList<>();
        boolean isSucss = true;
        for(SwStInvalidTicket item :swStInvalidTicketList){
            //0:未出库 1:已入库
            item.setOutputStatus("1");
            //出库
            SwStOutputDetail swStOutputDetail = new SwStOutputDetail();
            swStOutputDetail.setOutGoingType("9");
            swStOutputDetail.setOutGoingTime(DateUtilsSt.newLocalDateTime());
            swStOutputDetail.setTicketType(item.getTicketType());
            swStOutputDetail.setTicketName(item.getTicketName());
            swStOutputDetail.setTicketTimes(0);
            swStOutputDetail.setTicketPrice(new BigDecimal("0.00"));
            swStOutputDetail.setTicketNumber(1);
            swStOutputDetail.setTicketNo(item.getOldCardOrder());
            swStOutputDetail.setKeeper1code(user.getUserCode());
            swStOutputDetail.setKeeper1name(user.getNikeName());
            swStOutputDetail.setRemark(item.getRemark());
            swStOutputDetail.setDataSource("1");
            swStOutputDetail.setActiveTime(item.getActiveTime());
            //出库生成单号和更新基础表信息
            outputDetailServiceImpl.outputGenerateSingle(swStOutputDetail);
            list.add(swStOutputDetail);
            //swStOutputDetailService.save(swStOutputDetail);
            //QueryWrapper<SwStInvalidTicket> wrapper = new QueryWrapper<>();
            //wrapper.lambda().eq(SwStInvalidTicket::getId,item.getId());
            //SwStInvalidTicket swStInvalidTicket = new SwStInvalidTicket();
            //isSucss = this.update(swStInvalidTicket,wrapper);
        }
        //用于更新是否操作出库
        updateBatchById(swStInvalidTicketList);
        return swStOutputDetailService.saveBatch(list);
    }
}
